package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.UserBean;
import com.wantong.jianpu.models.OtherModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;
import com.wantong.jianpu.utils.XUtils;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/5/7
 * description:
 */
public class ChangePassViewModel extends BaseViewModel {
    public SingleLiveEvent<String> oldPass=new SingleLiveEvent<>();
    public SingleLiveEvent<String> nowPass=new SingleLiveEvent<>();
    public SingleLiveEvent<String> reNowPass=new SingleLiveEvent<>();
    public OtherModel model;
    public ChangePassViewModel(@NonNull Application application) {
        super(application);
        model=new OtherModel();
    }

    public BindingCommand commit=new BindingCommand(() -> {
       if(StringUtils.isEmpty(oldPass.getValue())){
           ToastUtils.show("请输入旧密码");
           return;
       }
        if(StringUtils.isEmpty(nowPass.getValue())){
            ToastUtils.show("请输入新密码");
            return;
        }
        if(StringUtils.isEmpty(reNowPass.getValue())){
            ToastUtils.show("请再次输入新密码");
            return;
        }

        if(!(nowPass.getValue().equals(reNowPass.getValue()))){
            ToastUtils.show("两次新密码不一致");
            return;
        }

        resetPass();
    });

    /**
     * 修改密码
     */
    public void resetPass(){
        Map<String,Object> map=new HashMap<>();
        map.put("userId", XUtils.getIntKey(Constants.USERID));
        map.put("password", nowPass.getValue());
        map.put("oldPassword", oldPass.getValue());
        showDialog();
        model.resetPwd(map, new BaseObserver<UserBean>() {
            @Override
            public void onSuccess(BaseResultBean<UserBean> t) {
                ToastUtils.show("修改成功");
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());

    }
}
